<?php

namespace app\index\controller;

use think\Controller;
use Myredis\Myredis;
use think\Db;
use think\Request;

class Attention extends Controller
{
    /**
     * 关注和取消关注
     * @param Request $request
     * @return \think\response\Json
     */
    public function index(Request $request)
    {
        $id = $request->session('uid');
        $fid = $request->post('fid');
        if (Myredis::sismber($id .'_friends', $fid)) {
            $res = Myredis::srem($id .'_friends', $fid);
            if ($res > 0) {
                $res = Db::table('xo_user_friends')->where([
                    'uid' => $id,
                    'fid' => $fid
                ])->delete();
                $data = [
                    'status' => false,
                    'info' => '取消关注成功'
                ];
            } else {
                $data = [
                    'status' => false,
                    'info' => '取消关注失败'
                ];
            }
        } else {
            $res = Myredis::sadd($id .'_friends', $fid);
            if ($res > 0) {
                $res = Db::table('xo_user_friends')->insert([
                    'uid' => $id,
                    'fid' => $fid
                ]);
                $data = [
                    'status' => true,
                    'info' => '关注成功'
                ];
            } else {
                $data = [
                    'status' => false,
                    'info' => '关注失败'
                ];
            }
        }

        return json($data);
    }

    /**
     * 执行取消关注
     * @param Request $request
     * @return \think\response\Json
     */
    public function unfollow(Request $request)
    {
        $id = $request->session('uid');
        $fid = $request->get('fid');
        $red = Myredis::srem($id .'_friends', $fid);
        $res = Db::table('xo_user_friends')->where([
                'uid' => $id,
                'fid' => $fid
            ])->delete();
        if($red && $res){
            $data = [
                'status'=>true,
                'info'=>'取消关注成功',
            ];
        }else{
            $data = [
              'status'=>false,
                'info'=>'取消关注失败'

            ];
        }
        return json($data);
    }

    /**
     * 执行关注 推荐好友界面
     * @param Request $request
     * @return \think\response\Json|void
     */
    public function attention(Request $request)
    {
        if(!Request::instance()->isAjax()){
            return $this->redirect('idnex/error/index');
        }
        $id = $request->session('uid');
        $fid = $request->get('fid');
        $red = Myredis::sadd($id .'_friends', $fid);
        $res = Db::table('xo_user_friends')->insert([
            'uid'=>$id,
            'fid'=>$fid
        ]);
        if($red && $res){
            $data = [
                'status'=>true,
                'info'=>'关注成功'
            ];
        }else{
            $data = [
                'status'=>false,
                'info'=>'关注失败'
            ];
        }
        return json($data);
    }
}


